﻿using System;
using System.Collections.ObjectModel;
using System.Collections.Generic;

namespace Elifart.Library
{
    [Serializable]
    public class AssociationCollection : ObservableCollection<Association>
    {
        #region Constructor

        public AssociationCollection()
        {
        }

        #endregion

        #region Attributes and Properties

        public Association this[string guid]
        {
            get
            {
                foreach (Association association in this)
                {
                    if (string.Compare(association.GUID, guid) == 0)
                        return association;
                }
                foreach (Association association in this)
                {
                    Association returnAssociation = association.Members[guid];
                    if (returnAssociation != null)
                        return returnAssociation;
                }

                return null;
            }
        }

        #endregion

        #region New Methods

        public new void Add(Association association)
        {
            Association parentAssociation = this[association.AffiliationGUID];
            if (parentAssociation != null)
                parentAssociation.Members.Add(association);
            else
                base.Add(association);
        }

        public new bool Remove(Association association)
        {
            Association parentAssociation = this[association.AffiliationGUID];
            if (parentAssociation != null)
                return parentAssociation.Members.Remove(association);
            else
                return base.Remove(association);
        }

        #endregion

        #region Private Methods

        private List<Association> GetAllAssociations(AssociationCollection associationCollection)
        {
            List<Association> associations = new List<Association>();
            foreach (Association association in associationCollection)
            {
                associations.Add(association);
                associations.AddRange(this.GetAllAssociations(association.Members));
            }
            return associations;
        }

        #endregion
    }
}